package work21;

import work20.Explode;
import work20.Missile;
import work20.Wall;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;

public class TankClient extends Frame{

    public static final int GAME_WIDTH=800;
    public static final int GAME_HEIGHT=600;


    Tank myTank = new Tank(50, 50, true, this);

    Wall w = new Wall(250,250,250,10,this);

    //new 出来了一辆敌方的坦克
    List<work20.Tank> tanks = new ArrayList<work20.Tank>();
    List<Missile> missiles = new ArrayList<Missile>();
    List<Explode> explodes = new ArrayList<Explode>();

    //这是一张虚拟图片

    Image offScreenImage = null;

    //paint这个方法不需要被调用，一旦要被重画的时候会被自动调用
    public void paint(Graphics g) {
        //看出容器中装了多少炮弹
        g.drawString("missiles count: " + missiles.size(), 10, 50);
        g.drawString("explodes count: " + explodes.size(), 10,70);
        g.drawString("tanks count: " + tanks.size(), 10, 90);

        for(int i = 0; i < explodes.size(); i++) {
            Explode e = explodes.get(i);
            e.draw(g);
        }

        w.draw(g);
        //将容器中的炮弹逐个画出来
        for(int i = 0; i < missiles.size(); i++) {
            Missile m = missiles.get(i);

            m.hitTanks(tanks);
            m.hitWall(w);
            m.draw(g);
        }

        myTank.draw(g);
        myTank.collidesWithWall(w);
        myTank.collidesWithTanks(tanks);
        for(int i = 0 ;i < tanks.size();i++){
            tanks.get(i).draw(g);
            tanks.get(i).collidesWithWall(w);
            tanks.get(i).collidesWithTanks(tanks);
        }
    }

    public void update(Graphics g){

        if(offScreenImage == null){
            offScreenImage = this.createImage(GAME_WIDTH,
                    GAME_HEIGHT);
        }
        //拿到这个图片的画笔
        Graphics gOffScreen = offScreenImage.getGraphics();


        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.GREEN);
        gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        gOffScreen.setColor(c);
        print(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }


    public void launchFrame() {
        for(int i = 0; i < 10; i++) {
            tanks.add(new Tank(50 + 40 * (i + 1), 50, false, this));
            tanks.get(i).collidesWithWall(w);
        }
        this.setLocation(300,50);
        this.setSize(GAME_WIDTH,GAME_HEIGHT);
        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setTitle("TankWar");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setResizable(false);
        this.setBackground(Color.GREEN);
        this.addKeyListener(new KeyMonitor());

        setVisible(true);

        new Thread(new PaintThread()).start();
    }

    public static void main(String[] args) {
        TankClient tc = new TankClient();
        tc.launchFrame();
    }



    private class PaintThread implements Runnable{
        public void run(){
            while(true){
                repaint();
                try{
                    Thread.sleep(50);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }

    }

    private class KeyMonitor extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            myTank.KyePressed(e);
        }
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);
        }
    }





}